package fmanager.kloon.dainb.ultilities;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import fmanager.kloon.dainb.dto.ListViewItem;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

public class Utils {
	private Context _context;
	public Utils(Context context){
		this._context = context;
	}
	//Reading file paths from sdCard
	public ArrayList<ListViewItem> getFilePaths(){
		ArrayList<ListViewItem> filePaths = new ArrayList<ListViewItem>();

		File directory = new File(android.os.Environment.getExternalStorageDirectory() + File.separator);
		showNotify(String.valueOf(directory.toString()));
		// Check for directory
		if (directory.isDirectory()) {
			// getting list of filepath
			File[] listFiles = directory.listFiles();
			// check for count
			if (listFiles.length > 0) {
				// loop through all files
				for (int i = 0; i < listFiles.length; i++) {
					// get file path
					//					String filePath = listFiles[i].getAbsolutePath();
					ListViewItem lvItem = new ListViewItem();
					lvItem.setName(listFiles[i].getName());
					lvItem.setSubItem(String.valueOf(listFiles[i].length()));
					lvItem.setDayMonthYear(ConvLongToDate(listFiles[i].lastModified()));

					filePaths.add(lvItem);
				}
			}else{
				//image directory is empty
				showNotify(" Folder is empty!");
			}
		}else{
			AlertDialog.Builder alert = new AlertDialog.Builder(this._context);
			alert.setTitle("Error!");
			alert.setMessage(" directory path is not valid!");
			alert.setPositiveButton("OK", null);
			alert.show();
		}
		return filePaths;
	}

	// Verify date
	private String ConvLongToDate(long _long){
		Date date=new Date(_long);
		SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yyyy");
		return df2.format(date);
	}

	// Get information from folder or file.

	/**
	 * getting screen width
	 * @param null
	 */
	public int getScreenWidth(){
		int columnWidth;
		WindowManager wm = (WindowManager)_context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		final Point point = new Point();
		try {
			display.getSize(point);
		} catch (java.lang.NoSuchMethodError e) {
			point.x = display.getWidth();
			point.y = display.getHeight();
		}
		columnWidth = point.x;
		return columnWidth;
	}
	private void showNotify(String message){
		Toast.makeText(this._context, message, Toast.LENGTH_LONG).show();
	}
}
